﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes;

namespace SharingCenter.Web.Admin.User
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            btnSearch.Click += new EventHandler(btnSearch_Click);
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            MakeSearchQueryAndRedirect();
        }

        private void MakeSearchQueryAndRedirect()
        {
            List<string> queryList = new List<string>();
            string nickname = txtNickName.Text.Trim();
            if (string.Empty != nickname)
            {
                queryList.Add("nn=" + nickname);
            }

            if (queryList.Count > 0)
            {
                Response.Redirect("Default.aspx" + "?s=1&" + string.Join(@"&", queryList.ToArray()), true);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ("1" == Request.QueryString["s"])
            {
                DoSearch();
            }
        }

        private void DoSearch()
        {
            using (scDataContext cxt = new scDataContext())
            {
                string query = string.Format("EXEC SearchUsers '', '{0}'", Request.QueryString["nn"]);
                List<ProfileMembership> users =  cxt.ExecuteQuery<ProfileMembership>(query).ToList();
                ListView1.DataSource = users;
                ListView1.DataBind();
            }
        }
    }
}
